# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS

from com.zzh.dao.declarDao import DeclarAPI

from . import app

CORS(app, supports_credentials=True)

'''申报车表 zzh_declar API'''


@app.route('/query-declar/<int:index>', methods=['POST'])  # 分页查询zzh_declar表。疑问：按照什么规则查询？？？,
def queryDeclar(index):
    """
    根据索引（页码）和用户id查询申报车列表
    :param index: 用户ID
    :return:
    """
    queryResultOfDatabaseDeclar, declarListJson = DeclarAPI().queryDeclar(index)  # 返回值的获取顺序要匹配
    if queryResultOfDatabaseDeclar:
        return jsonify({"declarListJson": declarListJson, "totalCount": queryResultOfDatabaseDeclar,
                        "message": {"code": 1, "info": "查询申报车成功"}})
    else:
        return jsonify({"message": {"code": 0, "info": "未查询到申报信息"}})


@app.route('/add-declar', methods=['POST'])
def addDeclar():
    if not request.json:
        print request.json
        abort(400)
    addDeclar = {
        'itemId': request.json.get('itemId', ),  # 必填，请传递item_id
        'userId': request.json.get('userId', ),  # 必填，请传递user_id
        'declarStatus': request.json.get('declarStatus', 0),
        'serviceId': request.json.get('serviceId', 0)

    }
    if not addDeclar:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    addDeclarResult = DeclarAPI().addDeclar(addDeclar)
    if addDeclarResult:
        return jsonify({"addDeclarInfo": addDeclar,
                        "massage": {"code": 1, "info": "addDeclar OK"}})  # 添加函数调用成功，返回添加的信息（不包括message_id）
    else:
        return jsonify({"message": {"code": 0, "info": "addDeclar Failed"}})  # 添加函数调用失败


@app.route('/update-declar/<int:declarId>', methods=['POST'])
def updateDeclar(declarId):
    if not request.json:
        print request.json
        abort(400)
    updateDeclar = {
        'itemId': request.json.get('itemId', ),  # 必填，请传递item_id
        'userId': request.json.get('cardName', ),  # 必填，请传递user_id
        'declarStatus': request.json.get('declarStatus', ),
        'serviceId': request.json.get('serviceId', 0)

    }
    if not updateDeclar:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    updateDeclarResult = DeclarAPI().updateDeclarById(updateDeclar, declarId)
    if updateDeclarResult:
        return jsonify({"updateDeclarInfo": updateDeclar,
                        "massage": {"code": 1, "info": "updateDeclar OK"}})  # 添加函数调用成功，返回添加的信息（不包括message_id）
    else:
        return jsonify({"message": {"code": 0, "info": "updateDeclar Failed"}})  # 添加函数调用失败


@app.route('/delete-declar/<int:declarId>', methods=['DELETE'])  # 根据invoiceId删除invoice表指定记录
def deleteDeclar(declarId):
    deleteDeclarResult = DeclarAPI().deleteDeclarById(declarId)
    if deleteDeclarResult:
        return jsonify({"message": {"code": 1, "info": "删除申报车记录成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "删除申报车记录失败！"}})


# --------批量删除申报车列表------------
@app.route('/delete-declars', methods=['POST'])
def delete_declars():
    if not request.json:
        abort(400)
    ids = []
    ids = request.json['declar_ids']
    DeclarAPI().delete_declars(ids)

    return jsonify({"message": {"code": 1, "info": "OK"}})
